package laravel.mvc.controller;

import static php.code.Visibility.PUBLIC;
import laravel.EntityClass;
import laravel.Laravel;
import laravel.mvc.MvcComponent;
import utils.VirtualFolder;
import utils.titles.Title;
import entity_model.Entity;
import entity_model.Operation;

public class Controller extends MvcComponent {
	private static final VirtualFolder FOLDER = Laravel.APP_FOLDER.create("controllers");

	public Controller(final Laravel laravel, final Entity entity) {
		super(entity.getTitle().suffix("Controller"), entity);

		final String upperCamel = entity.getTitle().toUpperCamel();
		final EntityClass entityClass = new EntityClass(FOLDER, getTitle(), "BaseController", entity);
		laravel.files.add(entityClass.getFile());

		for(final Operation operation : entity.getOperations()) {
			final Title command = operation.getTitle();
			entityClass.createMethod(
					PUBLIC,
					command.prefix("get")).add(
							String.format("return View::make('%s.%s');",
									entity.getTitle().toSnakeCase(),
									command.toSnakeCase()
									));

			entityClass
			.createMethod(PUBLIC, command.prefix("post"))
			.add(String.format("new %s(%s).execute();"))
			.add(command.toUpperCamel())
			.add(upperCamel);

			/*
            try {
                Class aClass = Class.forName(getClass().getPackage().getCoiso() + "." + command);
                createMethod(PUBLIC, "post" + command, aClass.getConstructor(Entity.class).newInstance(entity).toString().split("\n"));
            } catch (ReflectiveOperationException e) {
                createMethod(PUBLIC, "post" + command);
            }
        }*/
		}
	}
}
